/*
 * ClientManager.h
 *
 *  Created on: 2012-3-25
 *      Author: zgx
 */

#ifndef CLIENTMANAGER_H_
#define CLIENTMANAGER_H_
#include <list>
#include"util/Condition.h"

typedef enum CmdType {
    CMD_SETUP = 0,
    CMD_PLAY = 1,
    CMD_PAUSE = 2,
    CMD_STOP = 9,
    CMD_EXIT = 10,
    CMD_CAMERA_UP = 11,
    CMD_CAMERA_DOWN = 12,
    CMD_CAMERA_LEFT = 13,
    CMD_CAMERA_RIGHT = 14,
    CMD_STOP_CAMERA = 101
};
typedef enum _ClientState {
    STATE_NEW = 0,
    STATE_PENDDING = 1,
    STATE_RUNNING = 2,
    STATE_PAUSE = 3,
    STATE_STOP = 4,
    STATE_UNKNOWN
} CClientState;

typedef enum _ClientType {
    TYPE_TCP = 1, TYPE_UDP = 2
} CClientType;

typedef struct {
    int cmd;
    int type;
    int port;
    int sock;
    int state;
    char strClientAddr[20];
    char session[20];
} ClientNode;
typedef ClientNode* PClientNode;
typedef std::list<PClientNode> ClientList;
typedef std::list<PClientNode>::iterator ClientListIterator;

static CCondition sListCond;

class CClientManager {
public:
    CClientManager();
    virtual ~CClientManager();

public:
    int createNewClient(int cmd, int type, int port, char* strClientAddr,
            char* session);
    int manageClient(int cmd, char* session);
    void startAll();
    void sendToClient(unsigned char* buf, unsigned int len);
    int sizeOfAll() {
        return mTotalSize;
    }
    int sizeOfRunning() {
        return mRunningSize;
    }
    int sizeOfPause() {
        return mPauseSize;
    }
    int sizeOfPendding() {
        return mPenddingSize;
    }
    int sizeOfNew() {
        return mNewSize;
    }
private:
    int removeClient(char* session);
    int playClient(char* session);
    int pauseClient(char* session);
    int stopClient(char* session);

private:
    int createNewClient(const PClientNode &pClientNode);
    int connectToClient(char* strClientAddr, int port, int type);
    void decrease(int state) {
        switch (state) {
        case STATE_NEW:
            mNewSize--;
            break;
        case STATE_PAUSE:
            mPauseSize--;
            break;
        case STATE_PENDDING:
            mPenddingSize--;
            break;
        case STATE_RUNNING:
            mRunningSize--;
            break;
        case STATE_STOP:
        case STATE_UNKNOWN:
            break;
        }
    }

private:
    ClientList mClientList;
    int mRunningSize, mPauseSize, mPenddingSize, mNewSize, mTotalSize;
};
int camera_cmd(const char *serverAddr,int cmd);
#endif /* CLIENTMANAGER_H_ */
